home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / builtins / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-06  |  14.0 KB  |  604 lines

  1. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU Bash, the Bourne Again SHell.
  4.  
  5.    Bash is free software; you can redistribute it and/or modify it under
  6.    the terms of the GNU General Public License as published by the Free
  7.    Software Foundation; either version 1, or (at your option) any later
  8.    version.
  9.  
  10.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  11.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13.    for more details.
  14.    
  15.    You should have received a copy of the GNU General Public License along
  16.    with Bash; see the file COPYING.  If not, write to the Free Software
  17.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #if defined (HAVE_VPRINTF)
  22. #include <varargs.h>
  23. #endif /* VPRINTF */
  24.  
  25. #include "../shell.h"
  26. #include "../unwind_prot.h"
  27. #include "../maxpath.h"
  28. #include "../jobs.h"
  29. #include "../builtins.h"
  30. #include "hashcom.h"
  31.  
  32. void no_args (), remember_args (), parse_and_execute_cleanup ();
  33. extern int follow_symbolic_links, interactive;
  34.  
  35. /* Read a numeric arg for this_command_name, the name of the shell builtin
  36.    that wants it.  LIST is the word list that the arg is to come from. */
  37. int
  38. get_numeric_arg (list)
  39.      WORD_LIST *list;
  40. {
  41.   int count = 1;
  42.  
  43.   if (list)
  44.     {
  45.       if (sscanf (list->word->word, "%d", &count) != 1)
  46.     {
  47.       builtin_error ("bad non-numeric arg `%s'", list->word->word);
  48.       throw_to_top_level ();
  49.     }
  50.       no_args (list->next);
  51.     }
  52.   return (count);
  53. }
  54.  
  55. /* This is a lot like report_error (), but it is for shell builtins
  56.    instead of shell control structures, and it won't ever exit the
  57.    shell. */
  58. #if defined (HAVE_VPRINTF)
  59. /* VARARGS */
  60. builtin_error (va_alist)
  61.      va_dcl
  62. {
  63.   extern char *this_command_name;
  64.   char *format;
  65.   va_list args;
  66.  
  67.   if (this_command_name && *this_command_name)
  68.     fprintf (stderr, "%s: ", this_command_name);
  69.  
  70.   va_start (args);
  71.   format = va_arg (args, char *);
  72.   vfprintf (stderr, format, args);
  73.   va_end (args);
  74.   fprintf (stderr, "\n");
  75. }
  76.  
  77. #else /* Without VARARGS. */
  78. builtin_error (format, arg1, arg2, arg3, arg4, arg5)
  79.      char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
  80. {
  81.   extern char *this_command_name;
  82.  
  83.   if (this_command_name && *this_command_name)
  84.     fprintf (stderr, "%s: ", this_command_name);
  85.  
  86.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  87.   fprintf (stderr, "\n");
  88.   fflush (stderr);
  89. }
  90. #endif /* HAVE_VPRINTF */
  91.  
  92. /* Remember LIST in $0 ... $9, and REST_OF_ARGS.  If DESTRUCTIVE is
  93.    non-zero, then discard whatever the existing arguments are, else
  94.    only discard the ones that are to be replaced. */
  95. void
  96. remember_args (list, destructive)
  97.      WORD_LIST *list;
  98.      int destructive;
  99. {
  100.   register int i;
  101.   extern WORD_LIST *copy_word_list ();
  102.  
  103.   for (i = 1; i < 10; i++)
  104.     {
  105.       if (destructive && dollar_vars[i])
  106.     {
  107.       free (dollar_vars[i]);
  108.       dollar_vars[i] = (char *)NULL;
  109.     }
  110.  
  111.       if (list)
  112.     {
  113.       if (!destructive && dollar_vars[i])
  114.         free (dollar_vars[i]);
  115.  
  116.       dollar_vars[i] = savestring (list->word->word);
  117.       list = list->next;
  118.     }
  119.     }
  120.  
  121.   /* If arguments remain, assign them to REST_OF_ARGS. */
  122.   if (!list)
  123.     {
  124.       dispose_words (rest_of_args);
  125.       rest_of_args = NULL;
  126.     }
  127.   else
  128.     {
  129.       rest_of_args = (WORD_LIST *)copy_word_list (list);
  130.     }
  131. }
  132.  
  133. /* Return if LIST is NULL else barf and jump to top_level. */
  134. void
  135. no_args (list)
  136.      WORD_LIST *list;
  137. {
  138.   if (list)
  139.     {
  140.       builtin_error ("extra arguments");
  141.       longjmp (top_level, DISCARD);
  142.     }
  143. }
  144.  
  145. /* Return the octal number parsed from STRING, or -1 to indicate
  146.    that the string contained a bad number. */
  147. int
  148. read_octal (string)
  149.      char *string;
  150. {
  151.   int result = 0;
  152.   int digits = 0;
  153.  
  154.   while (*string && *string >= '0' && *string < '8')
  155.     {
  156.       digits++;
  157.       result = (result * 8) + *string++ - '0';
  158.     }
  159.  
  160.   if (!digits || result > 0777 || *string)
  161.     result = -1;
  162.  
  163.   return (result);
  164. }
  165.  
  166. /* Temporary static. */
  167. char *dotted_filename = (char *)NULL;
  168.  
  169. /* Return the full pathname that FILENAME hashes to.  If FILENAME
  170.    is hashed, but data->check_dot is non-zero, check ./FILENAME
  171.    and return that if it is executable. */
  172. char *
  173. find_hashed_filename (filename)
  174.      char *filename;
  175. {
  176.   extern HASH_TABLE *hashed_filenames;
  177.   extern int hashing_disabled;
  178.   register BUCKET_CONTENTS *item;
  179.  
  180.   if (hashing_disabled)
  181.     return ((char *)NULL);
  182.  
  183.   item = find_hash_item (filename, hashed_filenames);
  184.  
  185.   if (item)
  186.     {
  187.       /* If this filename is hashed, but `.' comes before it in the path,
  188.      then see if `./filename' is an executable. */
  189.       if (pathdata(item)->check_dot)
  190.     {
  191.       if (dotted_filename)
  192.         free (dotted_filename);
  193.  
  194.       dotted_filename = (char *)xmalloc (3 + strlen (filename));
  195.       strcpy (dotted_filename, "./");
  196.       strcat (dotted_filename, filename);
  197.  
  198.       if (executable_file (dotted_filename))
  199.         return (dotted_filename);
  200.  
  201.       /* Watch out.  If this file was hashed to "./filename", and
  202.          "./filename" is not executable, then return NULL. */
  203.  
  204.       /* Since we already know "./filename" is not executable, what
  205.          we're really interested in is whether or not the `path'
  206.          portion of the hashed filename is equivalent to the current
  207.          directory, but only if it starts with a `.'.  (This catches
  208.          ./. and so on.)  same_file () is in execute_cmd.c; it tests
  209.          general Unix file equivalence -- same device and inode. */
  210.       {
  211.         char *path = pathdata (item)->path;
  212.  
  213.         if (*path == '.')
  214.           {
  215.         int same = 0;
  216.         char *rindex (), *tail;
  217.  
  218.         tail = rindex (path, '/');
  219.  
  220.         if (tail)
  221.           {
  222.             *tail = '\0';
  223.             same = same_file
  224.               (".", path, (struct stat *)NULL, (struct stat *)NULL);
  225.             *tail = '/';
  226.           }
  227.         if (same)
  228.           return ((char *)NULL);
  229.           }
  230.       }
  231.     }
  232.       return (pathdata (item)->path);
  233.     }
  234.   else
  235.     return ((char *)NULL);
  236. }
  237.  
  238. /* **************************************************************** */
  239. /*                                    */
  240. /*            Pushing and Popping a Context            */
  241. /*                                    */
  242. /* **************************************************************** */
  243.  
  244. WORD_LIST **dollar_arg_stack = (WORD_LIST **)NULL;
  245. int dollar_arg_stack_slots = 0;
  246. int dollar_arg_stack_index = 0;
  247.  
  248. void push_dollar_vars (), pop_dollar_vars ();
  249.  
  250. void
  251. push_context ()
  252. {
  253.   extern int variable_context;
  254.  
  255.   push_dollar_vars ();
  256.   variable_context++;
  257. }
  258.  
  259. void
  260. pop_context ()
  261. {
  262.   extern int variable_context;
  263.  
  264.   pop_dollar_vars ();
  265.   kill_all_local_variables ();
  266.   variable_context--;
  267. }
  268.  
  269. /* Save the existing positional parameters on a stack. */
  270. void
  271. push_dollar_vars ()
  272. {
  273.   extern WORD_LIST *list_rest_of_args ();
  274.  
  275.   if (dollar_arg_stack_index + 2 > dollar_arg_stack_slots)
  276.     {
  277.       if (!dollar_arg_stack)
  278.     {
  279.       dollar_arg_stack =
  280.         (WORD_LIST **)xrealloc (dollar_arg_stack,
  281.                     (dollar_arg_stack_slots += 10)
  282.                     * sizeof (WORD_LIST **));
  283.     }
  284.     }
  285.   dollar_arg_stack[dollar_arg_stack_index] = list_rest_of_args ();
  286.   dollar_arg_stack[++dollar_arg_stack_index] = (WORD_LIST *)NULL;
  287. }
  288.  
  289. /* Restore the positional parameters from our stack. */
  290. void
  291. pop_dollar_vars ()
  292. {
  293.   if (!dollar_arg_stack || !dollar_arg_stack_index)
  294.     return;
  295.  
  296.   remember_args (dollar_arg_stack[--dollar_arg_stack_index], 1);
  297.   dispose_words (dollar_arg_stack[dollar_arg_stack_index]);
  298.   dollar_arg_stack[dollar_arg_stack_index] = (WORD_LIST *)NULL;
  299. }
  300.  
  301. /* Function called when one of the builtin commands detects a bad
  302.    option. */
  303. bad_option (s)
  304.      char *s;
  305. {
  306.   builtin_error ("unknown option: %s", s);
  307. }
  308.  
  309. /* Return a consed string which is the current working directory.
  310.    FOR_WHOM is the name of the caller for error printing.  */
  311. char *the_current_working_directory = (char *)NULL;
  312.  
  313. char *
  314. get_working_directory (for_whom)
  315.      char *for_whom;
  316. {
  317.   if (!follow_symbolic_links)
  318.     {
  319.       if (the_current_working_directory)
  320.     free (the_current_working_directory);
  321.  
  322.       the_current_working_directory = (char *)NULL;
  323.     }
  324.  
  325.   if (!the_current_working_directory)
  326.     {
  327.       char *directory, *getwd ();
  328.  
  329.       the_current_working_directory = (char *)xmalloc (MAXPATHLEN);
  330.       directory = getwd (the_current_working_directory);
  331.       if (!directory)
  332.     {
  333.       fprintf (stderr, "%s: %s\n\r",
  334.            for_whom, the_current_working_directory);
  335.       free (the_current_working_directory);
  336.       the_current_working_directory = (char *)NULL;
  337.       return (char *)NULL;
  338.     }
  339.     }
  340.  
  341.   return (savestring (the_current_working_directory));
  342. }
  343.  
  344. #if defined (JOB_CONTROL)
  345. /* Return the job spec found in LIST. */
  346. get_job_spec (list)
  347.      WORD_LIST *list;
  348. {
  349.   register char *word;
  350.   int job = NO_JOB;
  351.   int substring = 0;
  352.  
  353.   if (!list)
  354.     return (current_job);
  355.  
  356.   word = list->word->word;
  357.  
  358.   if (!*word)
  359.     return (current_job);
  360.  
  361.   if (*word == '%')
  362.     word++;
  363.  
  364.   if (digit (*word) && (sscanf (word, "%d", &job) == 1))
  365.     return (job - 1);
  366.  
  367.   switch (*word)
  368.     {
  369.     case 0:
  370.     case '%':
  371.     case '+':
  372.       return (current_job);
  373.  
  374.     case '-':
  375.       return (previous_job);
  376.  
  377.     case '?':            /* Substring search requested. */
  378.       substring++;
  379.       word++;
  380.       goto find_string;
  381.  
  382.     default:
  383.     find_string:
  384.       {
  385.     register int i, wl = strlen (word);
  386.     for (i = 0; i < job_slots; i++)
  387.       {
  388.         if (jobs[i])
  389.           {
  390.         register PROCESS *p = jobs[i]->pipe;
  391.         extern char *strindex ();
  392.         do
  393.           {
  394.             if ((substring && strindex (p->command, word)) ||
  395.             (strncmp (p->command, word, wl) == 0))
  396.               if (job != NO_JOB)
  397.             {
  398.               builtin_error ("ambigious job spec: %s", word);
  399.               return (DUP_JOB);
  400.             }
  401.               else
  402.             job = i;
  403.  
  404.             p = p->next;
  405.           }
  406.         while (p != jobs[i]->pipe);
  407.           }
  408.       }
  409.     return (job);
  410.       }
  411.     }
  412. }
  413. #endif /* JOB_CONTROL */
  414.  
  415. /* The command name of the currently running function. */
  416. extern char *this_command_name;
  417.  
  418. int parse_and_execute_level = 0;
  419.  
  420. /* How to force parse_and_execute () to clean up after itself. */
  421. void
  422. parse_and_execute_cleanup ()
  423. {
  424.   run_unwind_frame ("parse_and_execute_top");
  425. }
  426.  
  427. /* Parse and execute the commands in STRING.  Returns whatever
  428.    execute_command () returns.  This frees STRING. */
  429. int
  430. parse_and_execute (string, from_file)
  431.      char *string;
  432.      char *from_file;
  433. {
  434.   extern int remember_on_history;
  435.   extern int history_expansion_inhibited;
  436.   extern int indirection_level;
  437.   extern int builtin_pipe_in, builtin_pipe_out;
  438.   extern COMMAND *global_command;
  439.   extern char *indirection_level_string ();
  440.   extern int pop_stream (), free ();
  441.  
  442.   int last_result = EXECUTION_SUCCESS;
  443.   int code, jump_to_top_level = 0;
  444.   char *orig_string = string;
  445.  
  446.   /* Unwind protect this invocation of parse_and_execute (). */
  447.   begin_unwind_frame ("parse_and_execute_top");
  448.     unwind_protect_int (parse_and_execute_level);
  449.     unwind_protect_jmp_buf (top_level);
  450.     unwind_protect_int (indirection_level);
  451.     unwind_protect_int (interactive);
  452.     unwind_protect_int (remember_on_history);
  453.     unwind_protect_int (history_expansion_inhibited);
  454.     add_unwind_protect (pop_stream, (char *)NULL);
  455.     if (orig_string)
  456.       add_unwind_protect (free, orig_string);
  457.   end_unwind_frame ();
  458.  
  459.   parse_and_execute_level++;
  460.   push_stream ();
  461.   interactive = 0;
  462.   indirection_level++;
  463.  
  464.   /* We don't remember text read by the shell this way on
  465.      the history list, and we don't use !$ in shell scripts. */
  466.   remember_on_history = 0;
  467.   history_expansion_inhibited = 1;
  468.  
  469.   with_input_from_string (string, from_file);
  470.   {
  471.     extern char *yy_input_dev;
  472.     COMMAND *command;
  473.  
  474.     while (*yy_input_dev)
  475.       {
  476.     if (interrupt_state)
  477.       {
  478.         last_result = EXECUTION_FAILURE;
  479.         break;
  480.       }
  481.  
  482.     /* Provide a location for functions which `longjmp (top_level)' to
  483.        jump to.  This prevents errors in substitution from restarting
  484.        the reader loop directly, for example. */
  485.     if (code = setjmp (top_level))
  486.       {
  487.         switch (code)
  488.           {
  489.           case FORCE_EOF:
  490.           case EXITPROG:
  491.         run_unwind_frame ("pe_dispose");
  492.         /* Remember to call longjmp (top_level) after the old
  493.            value for it is restored. */
  494.         jump_to_top_level = 1;
  495.         goto out;
  496.  
  497.           case DISCARD:
  498.         run_unwind_frame ("pe_dispose");
  499.         continue;
  500.  
  501.           default:
  502.         programming_error ("bad jump to top_level: %d", code);
  503.         break;
  504.           }
  505.       }
  506.       
  507.     if (parse_command () == 0)
  508.       {
  509.         if ((command = global_command) != (COMMAND *)NULL)
  510.           {
  511.         extern struct fd_bitmap *new_fd_bitmap ();
  512.         extern void dispose_fd_bitmap ();
  513.         struct fd_bitmap *bitmap;
  514.  
  515.         bitmap = new_fd_bitmap (FD_BITMAP_SIZE);
  516.         begin_unwind_frame ("pe_dispose");
  517.         add_unwind_protect (dispose_fd_bitmap, bitmap);
  518.  
  519.         global_command = (COMMAND *)NULL;
  520.  
  521.         if (builtin_pipe_in != NO_PIPE)
  522.           bitmap->bitmap[builtin_pipe_in] = 1;
  523.  
  524.         if (builtin_pipe_out != NO_PIPE)
  525.           bitmap->bitmap[builtin_pipe_out] = 1;
  526.  
  527.         last_result =
  528.           execute_command_internal
  529.             (command, 0, NO_PIPE, NO_PIPE, bitmap);
  530.  
  531.         dispose_command (command);
  532.         run_unwind_frame ("pe_dispose");
  533.           }
  534.       }
  535.     else
  536.       {
  537.         last_result = EXECUTION_FAILURE;
  538.  
  539.         /* Since we are shell compatible, syntax errors in a script
  540.            abort the execution of the script.  Right? */
  541.         break;
  542.       }
  543.       }
  544.   }
  545.  
  546.  out:
  547.  
  548.   run_unwind_frame ("parse_and_execute_top");
  549.  
  550.   if (interrupt_state && parse_and_execute_level == 0)
  551.     throw_to_top_level ();
  552.  
  553.   if (jump_to_top_level)
  554.     longjmp (top_level, code);
  555.  
  556.   return (last_result);
  557. }
  558.  
  559. /* Perform a binary search and return the address of the builtin function
  560.    whose name is NAME.  If the function couldn't be found, or the builtin
  561.    is disabled or has no function associated with it, return NULL. */
  562. Function *
  563. find_shell_builtin (name)
  564.     char *name;
  565. {
  566.   int hi, lo, mid, j;
  567.  
  568.   hi = num_shell_builtins - 1;
  569.   lo = 0;
  570.  
  571.   while (lo <= hi)
  572.     {
  573.       mid = (lo + hi) / 2;
  574.       j = strcmp (shell_builtins[mid].name, name);
  575.       if (j == 0)
  576.     {
  577.       if (shell_builtins[mid].function && shell_builtins[mid].enabled)
  578.         return (shell_builtins[mid].function);
  579.       else
  580.         return ((Function *)NULL);
  581.     }
  582.       if (j > 0)
  583.     hi = mid - 1;
  584.       else
  585.     lo = mid + 1;
  586.     }
  587.   return ((Function *)NULL);
  588. }
  589.  
  590. static int
  591. shell_builtin_compare (sbp1, sbp2)
  592.      struct builtin *sbp1, *sbp2;
  593. {
  594.   return (strcmp (sbp1->name, sbp2->name));
  595. }
  596.  
  597. /* Sort the table of shell builtins so that the binary search will work
  598.    in find_shell_builtin. */
  599. initialize_shell_builtins ()
  600. {
  601.   qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
  602.     shell_builtin_compare);
  603. }
  604.